home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / jre.c < prev    next >
C/C++ Source or Header  |  1998-09-15  |  2KB  |  102 lines

  1. /*
  2.  * @(#)jre.c    1.5 98/07/01
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. /*
  16.  * Portable JRE Support functions.
  17.  */
  18.  
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include <jni.h>
  22. #include "jre.h"
  23.  
  24. /*
  25.  * Exits the runtime with the specified error message.
  26.  */
  27. void
  28. JRE_FatalError(JNIEnv *env, const char *msg)
  29. {
  30.     if ((*env)->ExceptionOccurred(env)) {
  31.     (*env)->ExceptionDescribe(env);
  32.     }
  33.     (*env)->FatalError(env, msg);
  34. }
  35.  
  36. /*
  37.  * Parses a runtime version string. Returns 0 if the successful, otherwise
  38.  * returns -1 if the format of the version string was invalid.
  39.  */
  40. jint
  41. JRE_ParseVersion(const char *ver, char **majorp, char **minorp, char **microp)
  42. {
  43.     int n1 = 0, n2 = 0, n3 = 0;
  44.  
  45.     sscanf(ver, "%*[0-9]%n.%*[0-9]%n.%*[0-9a-zA-Z]%n", &n1, &n2, &n3);
  46.     if (n1 == 0 || n2 == 0) {
  47.     return -1;
  48.     }
  49.     if (n3 != 0) {
  50.     if (n3 != strlen(ver)) {
  51.         return -1;
  52.     }
  53.     } else if (n2 != strlen(ver)) {
  54.     return -1;
  55.     }
  56.     *majorp = JRE_Malloc(n1 + 1);
  57.     strncpy(*majorp, ver, n1);
  58.     *minorp = JRE_Malloc(n2 - n1);
  59.     strncpy(*minorp, ver + n1 + 1, n2 - n1 - 1);
  60.     if (n3 != 0) {
  61.     *microp = JRE_Malloc(n3 - n2);
  62.     strncpy(*microp, ver + n2 + 1, n3 - n2 - 1);
  63.     }
  64.     return 0;
  65. }
  66.  
  67. /*
  68.  * Creates a version number string from the specified major, minor, and
  69.  * micro version numbers.
  70.  */
  71. char *
  72. JRE_MakeVersion(const char *major, const char *minor, const char *micro)
  73. {
  74.     char *ver = 0;
  75.  
  76.     if (major != 0 && minor != 0) {
  77.     int len = strlen(major) + strlen(minor);
  78.     if (micro != 0) {
  79.         ver = JRE_Malloc(len + strlen(micro) + 3);
  80.         sprintf(ver, "%s.%s.%s", major, minor, micro);
  81.     } else {
  82.         ver = JRE_Malloc(len + 2);
  83.         sprintf(ver, "%s.%s", major, minor);
  84.     }
  85.     }
  86.     return ver;
  87. }
  88.  
  89. /*
  90.  * Allocate memory or die.
  91.  */
  92. void *
  93. JRE_Malloc(size_t size)
  94. {
  95.     void *p = malloc(size);
  96.     if (p == 0) {
  97.     perror("malloc");
  98.     exit(1);
  99.     }
  100.     return p;
  101. }
  102.